<?php include('scripts.php'); ?>

<script type="text/javascript">
    $(function() {
        $("#transferForm").validate({
                    messages : {
                        transactionPassword: {
                            remote: "Transaction Password is not valid."
                        }
                    },
                    rules : {
                        "sponsorId" : {
                            required: true
                            //, minlength : 8
                        },
                        "ecashAmount" : {
                            required : true
                        },
                        "transactionPassword" : {
                            required : true,
                            remote: "/member/verifyTransactionPassword"
                        }
                    },
                    submitHandler: function(form) {
                        waiting();
                        var amount = $('#ecashAmount').autoNumericGet();
                        var ecashBalance = $('#ecashBalance').autoNumericGet();

                        if (parseFloat(ecashBalance) < (parseFloat(amount) + parseFloat($("#processFee").val()))) {
                            alert("<?php echo __("In-sufficient E-Cash")?>");
                            return false;
                        }

                        $("#ecashAmount").val(amount);
                        form.submit();
                    }
                });

        $("#sponsorId").change(function() {
            if ($.trim($('#sponsorId').val()) != "") {
                verifySponsorId();
            }
        });

        $('#ecashAmount').autoNumeric({
                    mDec: 2
                });
    });

    function verifySponsorId() {
        waiting();
        $.ajax({
                    type : 'POST',
                    url : "/member/verifySponsorId",
                    dataType : 'json',
                    cache: false,
                    data: {
                        sponsorId : $('#sponsorId').val()
                    },
                    success : function(data) {
                        if (data == null || data == "") {
                            alert("Invalid sponsor ID");
                            $('#sponsorId').focus();
                            $("#sponsorName").html("");
                        } else {
                            $.unblockUI();
                            $("#sponsorName").html(data.nickname);
                        }
                    },
                    error : function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("Your login attempt was not successful. Please try again.");
                    }
                });
    }
</script>

<form action="/member/transferEcash" id="transferForm" method="post">
    <input type="hidden" id="processFee" value="<?php echo $processFee?>">

    <div style="padding: 10px; top: 30px; width: 98%">
        <ul class="tabs-nav" id="_tab">
            <li class="tabs-unselected"><a href="/member/bonusDetails" name="tab"
                                           rel="pass"><span><?php echo __('Bonus Details'); ?></span></a></li>
            <li class="tabs-unselected"><a href="/member/ecashLog" name="tab"
                                           rel="info"><span><?php echo __('E-Cash Log'); ?></span></a></li>
            <li class="tabs-selected"><a href="/member/transferEcash" name="tab"
                                         rel="pass"><span><?php echo __('E-Cash Transfer'); ?></span></a></li>
        </ul>
        <div class="tabs-panel" style="width:565px; padding:10px 10px;">
            <h3><?php echo __('E-Cash Transfer') ?></h3>
            <table cellpadding="3" cellspacing="3" border="0" width="100%" class="tablelist">
                <tr>
                    <td colspan=2 align='center'>
                        <?php if ($sf_flash->has('successMsg')): ?>
                        <div class="ui-widget">
                            <div style="margin-top: 20px; padding: 0 .7em;" class="ui-state-highlight ui-corner-all">
                                <p><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-info"></span>
                                    <strong><?php echo $sf_flash->get('successMsg') ?></strong></p>
                            </div>
                        </div>
                        <?php endif; ?>
                        <?php if ($sf_flash->has('errorMsg')): ?>
                        <div class="ui-widget">
                            <div style="margin-top: 20px; padding: 0 .7em;" class="ui-state-error ui-corner-all">
                                <p><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-alert"></span>
                                    <strong><?php echo $sf_flash->get('errorMsg') ?></strong></p>
                            </div>
                        </div>
                        <?php endif; ?>
                    </td>
                </tr>
                <tr>
                    <td>
                        <table cellpadding="0" cellspacing="0">
                            <tr>
                                <td width="160px" class="caption">
                                    <strong><?php echo __('Shareholder ID'); ?></strong>
                                </td>
                                <td class="value">
                                    <input name="sponsorId" type="text" id="sponsorId" tabindex="1" maxlength="8"/>
                                </td>
                            </tr>
                            <tr>
                                <td class="caption">
                                    <strong><?php echo __('Shareholder Name'); ?></strong>
                                </td>
                                <td class="value">
                                    <strong><span id="sponsorName"></span></strong>
                                </td>
                            </tr>
                            <tr>
                                <td class="caption">
                                    <strong><?php echo __('E-Cash Balance'); ?></strong>
                                </td>
                                <td class="value">
                                    <input name="ecashBalance" id="ecashBalance" tabindex="2" disabled="disabled"
                                           value="<?php echo number_format($ledgerAccountBalance, 2); ?>"/>
                                </td>
                            </tr>
                            <tr>
                                <td class="caption">
                                    <strong><?php echo __('Transfer E-Cash Amount'); ?></strong>
                                </td>
                                <td class="value">
                                    <input name="ecashAmount" id="ecashAmount" tabindex="3"/>
                                </td>
                            </tr>
                            <tr>
                                <td class="caption">
                                    <strong><?php echo __('Transaction Password'); ?></strong>
                                </td>
                                <td class="value">
                                    <input name="transactionPassword" type="password" id="transactionPassword"
                                           tabindex="4"/>
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>

                <tr>
                    <td colspan=3><font color="#dc143c"> <?php
                                if ($processFee != 0)
                        echo __('every transfer action need to pay USD%1%.00 processing fees', array('%1%' => $processFee));
                        ?></font></td>
                </tr>
                <!--<tr>
                            <td colspan='3' align='center'>
                            <?php /*if ($sf_flash->has('errorMsg')): */?>
                                <span class="error-font"><?php /*echo $sf_flash->get('errorMsg') */?></span>
                            <?php /*endif; */?>
                            <?php /*if ($sf_flash->has('successMsg')): */?>
                                <span class="font"><?php /*echo $sf_flash->get('successMsg') */?></span>
                            <?php /*endif; */?>
                            </td>
                        </tr>-->
                <tr>
                    <td colspan='3' align='center'>
                        <input type="submit" name="Button1" value="<?php echo __('Submit') ?>" language="javascript"
                               id="btnTransfer" tabindex="5"/>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
